package com.iraeta.service.gift.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import com.iraeta.Config;
import com.iraeta.common.Constants;
import com.iraeta.entity.FileEntityType;
import com.iraeta.entity.SysFiles;
import com.iraeta.entity.gift.Gift;
import com.iraeta.entity.resulttype.GiftResultType;
import com.iraeta.mapper.gift.GiftMapper;
import com.iraeta.service.gift.IGiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iraeta.service.sys.ISysFilesService;
import com.iraeta.uitl.Util;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 礼品表 服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2023-12-21
 */
@Service
public class GiftServiceImpl extends ServiceImpl<GiftMapper, Gift> implements IGiftService {
    @Resource
    ISysFilesService sysFilesService;
    @Autowired
    Config config;

    @Override
    public List<GiftResultType> listGiftByName(String searchName) {
        return baseMapper.listGiftByName(searchName);
    }

    @Override
    public List<GiftResultType> listGiftByTypeId(Integer typeId, String name){
        return baseMapper.listGiftByTypeId(typeId, name);
    }

    @Override
    public GiftResultType getGift(Integer id) {
        return baseMapper.getGift(id);
    }

    @Override
    public void deleteGift(Integer id) {
        Gift gift = new Gift();
        gift.setId(id);
        gift.setDeleted(1);
        baseMapper.updateById(gift);
    }

    @Override
    @Transactional
    public void tongBuGiftModel(int sourcegiftid, int targetgiftid) {
        Long sId = Long.parseLong(String.valueOf(sourcegiftid));
        Long tId = Long.parseLong(String.valueOf(targetgiftid));
        List<SysFiles> sourceSysFiles = sysFilesService.selectSysFilesList(FileEntityType.giftmodel, sId);
        if(sourceSysFiles.size() == 0){
            throw new RuntimeException("源礼品没有模版,无法作为模版源");
        }
        String baseDir = config.profile;
        for (SysFiles sourceSysFile : sourceSysFiles) {
            String realFilePath = baseDir + sourceSysFile.getFilePath().substring( Constants.RESOURCE_PREFIX.length());
            String businessNo = Util.getBusinessNo();
            String extName = FileNameUtil.extName(realFilePath);
            String copyFilePath = extractFilename(businessNo, extName);
            String copyRealFilePath = baseDir + File.separator + copyFilePath;
            FileUtil.copy(realFilePath, copyRealFilePath, true);

            SysFiles targetSysFile = new SysFiles(sourceSysFile.getEntityTypeId(), tId);
            targetSysFile.setCreateBy(sourceSysFile.getCreateBy());
            targetSysFile.setFileName(FileNameUtil.getName(copyRealFilePath));
            targetSysFile.setFilePath(Constants.RESOURCE_PREFIX  + "/" + copyFilePath);
            sysFilesService.save(targetSysFile);
        }

    }

    private static final String extractFilename(String businessNo, String extName)
    {
        return StrUtil.format("{}/{}.{}", DateUtil.format(new Date(), "yyyy/MM/dd"), businessNo, extName);
    }
}
